package com.mz.ozy.activity.assets;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.JdDetailAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.JDDetailBean;
import com.mz.ozy.mvp.presenter.JDDetailPresenter;
import com.mz.ozy.mvp.view.JDDetailContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 金豆明细
 */
public class JDDetailsActivity extends BaseActivity<JDDetailContract.View, JDDetailPresenter> implements JDDetailContract.View {

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private JdDetailAdapter mJdDetailAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<JDDetailBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jd_details);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setText(R.string.assets_jd_detail);
        tvBalance.setText(getIntent().getStringExtra("amount"));
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mJdDetailAdapter = new JdDetailAdapter(this, mBeanList);
        recyclerview.setAdapter(mJdDetailAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getJDDetail(JDDetailsActivity.this, String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getJDDetail(JDDetailsActivity.this, String.valueOf(pageNo));

            }
        });
    }


    @Override
    public void getJDDetailSuccess(JDDetailBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mJdDetailAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }
}
